package frame.PayMain;

import MyBatis.MyBatislianjian;
import frame.GuanliPanel;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.EmployeeMapper;
import org.example.mapper.PayMainMapper;
import org.example.pojo.Employee;
import org.example.pojo.PayMain;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class PayMainselect extends JInternalFrame {
    private JComboBox<String> eidComboBox; // 员工编号下拉框
    private JTextField[] inputFields = new JTextField[4]; // 输入框数组 (不包括员工编号)
    private JLabel[] inputLabels = new JLabel[4]; // 输入框说明标签数组
    private JTable resultTable; // 显示查询结果的表格
    private DefaultTableModel tableModel;
    private SqlSession sqlSession = MyBatislianjian.getSqlSession();
    private PayMainMapper payMainMapper = sqlSession.getMapper(PayMainMapper.class);
    private EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

    public PayMainselect() {
        setTitle("查询采购信息");
        setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建顶部的查询输入区域
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 10, 5, 10); // 设置组件间距

        // 初始化员工编号下拉框
        JLabel eidLabel = new JLabel("员工编号: ");
        eidLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(eidLabel, gbc);

        eidComboBox = new JComboBox<>();
        eidComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        inputPanel.add(eidComboBox, gbc);

        populateEidComboBox(); // 填充下拉框数据

        // 添加输入提示标签
        JLabel eidPromptLabel = new JLabel("选择员工编号");
        eidPromptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        eidPromptLabel.setForeground(Color.GRAY);
        gbc.gridx = 2;
        gbc.insets = new Insets(5, 10, 5, 10); // 重新设置间距
        inputPanel.add(eidPromptLabel, gbc);

        String[] labels = {"采购数量", "采购总价", "采购时间", "备注"};

        for (int i = 0; i < labels.length; i++) {
            inputLabels[i] = new JLabel(labels[i] + ": ");
            inputLabels[i].setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 0;
            gbc.gridy = i + 1;
            inputPanel.add(inputLabels[i], gbc);

            inputFields[i] = new JTextField(15);
            inputFields[i].setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            inputPanel.add(inputFields[i], gbc);

            // 添加输入提示标签
            JLabel promptLabel = new JLabel("输入你知道的采购信息查询");
            promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            promptLabel.setForeground(Color.GRAY);
            gbc.gridx = 2;
            gbc.insets = new Insets(5, 10, 5, 10); // 重新设置间距
            inputPanel.add(promptLabel, gbc);
        }

        add(inputPanel, BorderLayout.NORTH);

        // 创建表格模型
        String[] columnNames = {"采购编号", "员工编号", "采购数量", "采购总价", "采购时间", "备注"};
        tableModel = new DefaultTableModel(columnNames, 0);
        resultTable = new JTable(tableModel);
        resultTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        resultTable.setRowHeight(25);

        // 添加表格到滚动面板
        JScrollPane scrollPane = new JScrollPane(resultTable);
        add(scrollPane, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton searchButton = new JButton("搜索");
        searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performSearch();
            }
        });
        buttonPanel.add(searchButton);

        JButton returnButton = new JButton("返回");
        returnButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GuanliPanel.setContent(new PayMainPanel());
            }
        });
        buttonPanel.add(returnButton);

        add(buttonPanel, BorderLayout.SOUTH);

        // 设置窗口大小并显示
        pack();
        setVisible(true);
    }

    // 执行搜索的方法
    private void performSearch() {
        String eid = (String) eidComboBox.getSelectedItem(); // 从下拉框获取员工编号
        String pcountStr = inputFields[0].getText().trim();
        String ptotalStr = inputFields[1].getText().trim();
        String pdate = inputFields[2].getText().trim();
        String other = inputFields[3].getText().trim();

        // 处理特殊情况，如价格字段和数量字段
        Integer pcount = null;
        Float ptotal = null;
        try {
            if (!pcountStr.isEmpty()) {
                pcount = Integer.parseInt(pcountStr);
            }
            if (!ptotalStr.isEmpty()) {
                ptotal = Float.parseFloat(ptotalStr);
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "采购数量和采购总价字段必须是数字！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 创建查询对象
        PayMain payMainQuery = new PayMain(eid, pcount, ptotal, pdate, other);

        List<PayMain> payMains = payMainMapper.queryPayMain(payMainQuery);

        if (payMains.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有符合条件的采购信息！", "提示", JOptionPane.INFORMATION_MESSAGE);
        }

        updateTableData(payMains);
    }

    // 更新表格数据的方法
    private void updateTableData(List<PayMain> payMains) {
        // 清空表格
        tableModel.setRowCount(0);

        // 添加新的数据
        for (PayMain payMain : payMains) {
            Object[] row = {
                    payMain.getPid(), // Integer
                    payMain.getEid(), // String
                    payMain.getPcount() != null ? payMain.getPcount() : "N/A", // Integer -> String
                    payMain.getPtotal() != null ? String.format("%.2f", payMain.getPtotal()) : "N/A", // float -> String
                    payMain.getPdate(), // String
                    payMain.getOther() // String
            };
            tableModel.addRow(row);
        }
    }

    // 填充员工编号下拉框
    private void populateEidComboBox() {
        List<Employee> employees = employeeMapper.selectAll();
        eidComboBox.addItem(""); // 添加一个空选项
        for (Employee employee : employees) {
            eidComboBox.addItem(employee.getEid());
        }
    }
}
